package org.shop.oauth.service;

import org.apache.commons.lang.BooleanUtils;
import org.shop.oauth.dao.AuthorityDao;
import org.shop.oauth.dao.CredentialsDao;
import org.shop.oauth.entity.Authority;
import org.shop.oauth.entity.Credentials;
import org.shop.user.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired(required = false)
    private UserFeign userApi;

    @Autowired
    private CredentialsDao credentialsDao;
    @Autowired
    private AuthorityDao authorityDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Credentials credentials1=new Credentials();
        credentials1.setName(username);
        Credentials credentials = credentialsDao.selectOne(credentials1);
        if (credentials == null) {
            throw new UsernameNotFoundException("User '" + username + "' can not be found");
        }else{
            List<Authority> authorities=authorityDao.getAuthroitiesByCredential(credentials.getId());
            credentials.setAuthorityList(authorities);
        }

        return new User(credentials.getName(), credentials.getPassword(),credentials.getEnabled().equals("1")?true:false, true, true, true, credentials.getAuthorityList());
    }

    /****
     */
 /*   @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //=========================客户端信息认证start==================================
        //取出身份，如果身份为空说明没有认证
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //没有认证统一采用httpbasic认证，httpbasic中存储了client_id和client_secret，开始认证client_id和client_secret
        if (authentication == null) {
            // 查询数据库
            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
            if (clientDetails != null) {
                //秘钥
                String clientSecret = clientDetails.getClientSecret();
                //数据库查找方式
                return new User(username, clientSecret, AuthorityUtils.commaSeparatedStringToAuthorityList(""));
            }
        }
        //=========================客户端信息认证end==================================

        //=========================用户账号密码信息认证start==================================
        if (StringUtils.isEmpty(username)) {
            return null;
        }

        //根据用户名查询用户信息
//		String password = BCrypt.hashpw("123456", BCrypt.gensalt());
        // 通过数据库去查询用户通过密码授权
        Result<org.shop.user.pojo.User> dbUserResult = userApi.findById(username);
        org.shop.user.pojo.User dbUser=dbUserResult.getData();
        if (dbUser == null) {
            return null;
        }
        String password = dbUser.getPassword();
        //用户权限需要查询数据获取,这里写的假的作为演示数据
        String permissions = "ROLE_ADMIN,ROLE_USER"; //角色必须以ROLE_开头
        UserJwt userJwt = new UserJwt(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
        //=========================用户账号密码信息认证end==================================
        return userJwt;
    }*/

}
